package com.yunlongn.async.idempotent.filter;


import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

/**
 * 默认使用内存过滤，只适用于单节点
 *
 * @author yunlongn
 */
public class MemoryIdempotentFilter implements IdempotentFilter {

    private final static Object NULL = new Object();

    private final static ConcurrentMap<String, Object> ID_MAP = new ConcurrentHashMap<>();

    @Override
    public boolean needFilter(String id, TimeUnit timeUnit, int[] periods) {
        return ID_MAP.putIfAbsent(id, NULL) != null;
    }

    @Override
    public void remove(String id) {
        ID_MAP.remove(id);
    }

}
